import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:provider/provider.dart';
import 'package:terminal_flutter_demo/generated/l10n.dart';
import 'package:terminal_flutter_demo/pages/mobxpages/store/mobx_counter.dart';

class LocalizationDemoPage extends StatefulWidget {
  const LocalizationDemoPage({Key? key}) : super(key: key);

  @override
  _LocalizationDemoPageState createState() => _LocalizationDemoPageState();
}

class _LocalizationDemoPageState extends State<LocalizationDemoPage> {
  @override
  Widget build(BuildContext context) {
    return Consumer<MobxCounter>(
      builder: (context, mobxCounter, child) {
        return Observer(
          builder: (context) => Center(
            child: Column(
              children: [
                SwitchListTile(
                  value: mobxCounter.useLanguage == 'zh',
                  title: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(mobxCounter.useLanguage),
                      Text(S.of(context).title),
                    ],
                  ),
                  onChanged: (value) {
                    mobxCounter.setLanguage(
                        value: mobxCounter.useLanguage == 'zh' ? 'en' : 'zh');
                  },
                ),
                const SizedBox(height: 30),
                Text(S.of(context).method('tikeyc')),
                const SizedBox(height: 30),
                Text(S.of(context).title),
                const SizedBox(height: 30),
                Text(S.current.title),
                const SizedBox(height: 30),
                Localizations(
                  locale: const Locale.fromSubtags(
                      languageCode: 'en'), // 单个组件初始化为英文
                  delegates: const [
                    GlobalMaterialLocalizations.delegate,
                    GlobalWidgetsLocalizations.delegate,
                    GlobalCupertinoLocalizations.delegate,
                    S.delegate,
                  ],
                  child: Builder(
                    builder: (context) {
                      return Text(S.of(context).title);
                    },
                  ),
                ),
                const SizedBox(height: 30),
                Localizations.override(
                  context: context,
                  delegates: const [
                    GlobalMaterialLocalizations.delegate,
                    GlobalWidgetsLocalizations.delegate,
                    GlobalCupertinoLocalizations.delegate,
                    S.delegate,
                  ],
                  locale: const Locale.fromSubtags(
                      languageCode: 'en'), // 单个组件初始化为英文
                  child: Builder(
                    builder: (context) {
                      return Text(S.of(context).title);
                    },
                  ),
                ),
                const SizedBox(height: 30),
                CalendarDatePicker(
                  initialDate: DateTime.now(),
                  firstDate: DateTime(1900),
                  lastDate: DateTime(2100),
                  onDateChanged: (value) {},
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
